package com.gmail.dmitryustimov.ms.java7.patternslib.mediator;

import java.util.ArrayList;
import java.util.List;


public class EventPublisher<T> {

    private List<SubscriberAction<T>> subscribers = new ArrayList<>();
    private Boolean isPublicationSuspended = false;

    public void subscribe(SubscriberAction<T> subscriber) {
        subscribers.add(subscriber);
    }

    public void unsubscribe(SubscriberAction<T> subscriber) {
        subscribers.remove(subscriber);
    }

    public void publish(T eventData, Colleague colleague) {
        if (!isPublicationSuspended) {
            for (SubscriberAction<T> subscriber: subscribers) {
                if (subscriber.getHostColleague() != colleague) {
                    subscriber.process(eventData, colleague);
                }
            }
        }
    }

    public void suspendPublication() {
        isPublicationSuspended = true;
    }

    public void restorePublication() {
        isPublicationSuspended = false;
    }
}